/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ekaaty.business;

import br.com.ekaaty.bean.Evento;
import br.com.ekaaty.dao.EventoDao;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author otavio
 */
public class EventoBusiness implements GenericBusiness<Evento> {

    @Override
    public boolean vazio(Evento bean) {
        if (bean.getData() == null) {
            return true;
        } else if (bean.getDescricao() == null || bean.getDescricao().equals("")) {
            return true;
        } else if (bean.getLocal() == null || bean.getLocal().equals("")) {
            return true;
        } else if (bean.getNome() == null || bean.getNome().equals("")) {
            return true;
        }

        return false;
    }

    @Override
    public boolean duplicado(Evento bean) {
        EventoDao eventoDao;
        try {
            eventoDao = new EventoDao();
            return !eventoDao.ehUnico(bean);
        } catch (Exception ex) {
            Logger.getLogger(EventoBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
public boolean  update(Evento bean){
  
            EventoDao eventoDao;
            try {
                eventoDao = new EventoDao();

                eventoDao.atualizar(bean);
                return true;
            } catch (Exception ex) {
                Logger.getLogger(EventoBusiness.class.getName()).log(Level.SEVERE, null, ex);
            }

        
        return false;

}
    @Override
    public boolean adicionar(Evento bean) {
      
        if (!duplicado(bean) && !vazio(bean) && validado(bean)) {
            EventoDao eventoDao;
            try {
                eventoDao = new EventoDao();

                eventoDao.adicionar(bean);
                return true;
            } catch (Exception ex) {
                Logger.getLogger(EventoBusiness.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return false;
    }

    @Override
    public boolean validado(Evento bean) {
        if (bean.getNome() == null || bean.getDescricao() == null) {
            return false;
        }
        if (!dataValida(bean)) {
            return false;
        }
        if (bean.getNome().length() >= 4 || bean.getDescricao().length() >= 10) {
            return true;
        }
        return false;
    }

    public boolean dataValida(Evento bean) {

        Date d = new Date();
        Date d2 = bean.getData();
        if (bean.getData() == null || d.after(d2)) {
            if (!d.equals(d2)) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
}
